const fs = require("fs");
const path = require("path");

const isFileType = fileName => /.*(?=\.txt)/.test(fileName);
const isDir = fileName => /^[^\.]*$/.test(fileName);
const replaceFunc = fileName => fileName.replace(/-hello/, "");

fs.readdir(__dirname, (err, files) => {
  if (err) console.error(err);

  for (let fileName of files) {
    if (isFileType(fileName) || isDir(fileName)) {
      let filePath = path.join(__dirname, fileName);
      let newFileName = replaceFunc(fileName);
      fs.rename(filePath, path.join(__dirname, newFileName), err => {
        if (err) console.error(`文件 ${fileName} 重命名失败`);
        console.log(`文件 ${fileName} 重命名为 ${newFileName}`);
      });
    }
  }
});
